/*
	作者：AND 358122354
	功能：12生肖竞猜
*/
var status = 0;
var Tpe;
var Type;
var Name;
var Quantity;
var charId;
var haha;
var Current;
var Last;
var X = Array();
var XX = Array();
var XXX = Array();
var myDate = new Date();
var year = myDate.getFullYear();
var month = myDate.getMonth()+1;
var hour = myDate.getHours();
var day = myDate.getDate();
var HOUR = 21;//设置开奖后几点可以开始下注 默认：21
var Rank = 5;//设置显示几个期数的竞猜结果
var Count = 2;//设置可下注几次
var List = Array(//设置赌博币种，可开多种
Array("点卷",1000,10000,true,1,2,3),//币种，下注最小金额，下注最大金额，是否开启该币种进行下注,（后面1，2为当前赔率比1：2）
Array("抵用卷",1000,10000,false,1,2,3),//最后一位数字代表当前币种所能下注的限度（能下几注的限度，默认：3）
Array("金币",1000,10000,false,1,2,3),//注意：所有币种下投的最大值不能超过21E。否则出错
Array("余额",1000,10000,false,1,2,3)
);
var NameList = Array("鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪");
var NamePH = Array("#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#","#v4000000#");//这里设置
function start() {
    status = -1;
    action(1, 0, 0);
}

function action(mode, type, selection) {
    if (mode == 1) {
		status++;
	} else {
		if (status >= 1) {
			status = 1;
			cm.dispose();
			return;
		} else {
			cm.dispose();
			return;
		}
		status--;
	}
	var em = cm.getEventManager("Zodiac");
	charId = cm.getPlayer().getId();
	if (em==null){
		cm.sendOk("配置文件出错！");
		cm.dispose();
		return;
	}
	var ttt = "无";
	if (em.getProperty("check")=="0"){
		ttt="#r下注中#k\r\n";
	}else if (em.getProperty("check")=="1"){
		ttt="#r开奖中#k\r\n";
	}else if (em.getProperty("check")=="2"){
		ttt="#g领奖时间#k\r\n";
	}
	Current =em.getProperty("Zodiac");
	Last =em.getProperty("LastZodiac");
	var tt = "";
	var tttt="";
	if(getresult(Rank)){
		for (var i = 0;i<X.length ;i++ ){
			tttt += "第 #r["+X[i][1]+"]#k 期 开奖生肖 #b["+X[i][0]+"]#k\r\n";
		}
	}else{
		tttt += "#r当前无任何开奖信息！#k";
	}
	if(cm.getChannelNumber()!=1){
		Current="请到一线查看";
	}
    if (cm.getMapId() == 180000001) {
            cm.dispose();
    }else if (status == 0) {
		haha = (year+""+month+""+day)*1;
		if (getdate(charId,haha)){
			for (var i =0;i<XX.length ;i++ ){
				tt += "当期:#r["+XX[i][3]+"]#k 币种：#r["+List[XX[i][0]][0]+"]#k 生肖：#r["+NameList[XX[i][1]]+"]#k 金额：#r["+XX[i][2]+"]#k\r\n";
			}
		}else{
			tt += "当期无下注信息！\r\n";
		}
		var TXT = "#r			12生肖竞猜中心#k\r\n";
		TXT+="#r#L3#查看说明#l";
		TXT+="#r#L0#下注竞猜#l";
		TXT+="#r#L1#领取奖励#l\r\n"
		TXT+="#b#e当前状态#n#k："+ttt;
		TXT+="			#e【系统信息】#n\r\n";
		TXT+="当前赔率:";
		for (var i = 0 ;i<List.length ;i++ ){
			if (List[i][3]){
				TXT+=List[i][0]+"#b["+List[i][4]+":"+List[i][5]+"]#k";
			}
		}
		TXT+="\r\n"+tttt;
		//TXT+="\r\n上期开奖生肖："+Last;
		TXT+="\r\n本期开奖生肖："+Current;
		TXT+="\r\n\r\n";
		TXT+="		  #e  【我的下注信息】#n\r\n";
		TXT+=tt+"\r\n\r\n";
		if (cm.getChar().isGM()){
			TXT+="#r#L2#修改当天即将开奖的结果【GM可见】#l\r\n";
		}
        cm.sendSimple(TXT);
    } else if (status == 1) {
		var MyName = cm.getChar().getName();
		if(selection == 0){
			if(hour>=20&&hour<HOUR){
				cm.sendOk("现在不是下注时间！请"+HOUR+"点后过来！");
				cm.dispose();
				return;
			}
			if(hour>=HOUR){
				haha = (year+""+month+""+(day+1))*1;
			}
			if (!getcount(charId,haha)){
				cm.sendOk("出现未知错误！请联系作者");
				cm.dispose();
				return;
			}
			if (XXX[0]>=Count){
				cm.sendOk("对不起！今天已经下过"+Count+"注了哦！");
				cm.dispose();
				return;
			}
			Tpe = 0;
			var TXT = "		#b请选择要下注的币种（例如：点卷、抵用卷、金币）#k\r\n\r\n";
			for (var i = 0;i < List.length ;i++ ){
				if(List[i][3]){
					TXT+= "#L"+i+"#我选择 #r["+List[i][0]+"]#k 进行下注#l\r\n"
				}
			}
			cm.sendSimple(TXT);
		}else if(selection == 1){
			Tpe = 1;
			if (em.getProperty("check")!="2"){
				cm.sendOk("本期开奖时间还没开始，或已经结束！！");
				cm.dispose();
				return;
			}
			XXX[2] = 0;
			var TXT = "您要选择哪一注兑奖？\r\n";
			for (var i =0;i<XX.length ;i++ ){
				var TT="";
				if (XX[i][5]==1){
					TT="#e#b(已领取)#k#n";
				}
				if (Current==NameList[XX[i][1]]){
					TXT+= "#L"+i+"##e当期:#r["+XX[i][3]+"]#k 币种：#r["+List[XX[i][0]][0]+"]#k 生肖：#r["+NameList[XX[i][1]]+"]#k 金额：#r["+XX[i][2]+"]#k"+TT+"#l#n\r\n";
				}else{
					XXX[2] = XXX[2]+1;
					TXT+= "\r\n当期:#r["+XX[i][3]+"]#k 币种：#r["+List[XX[i][0]][0]+"]#k 生肖：#r["+NameList[XX[i][1]]+"]#k 金额：#r["+XX[i][2]+"]#k #b(未中奖)#k\r\n";
				}
			}
			cm.sendSimple(TXT);
		}else if(selection == 2){
			Tpe = 2;
			var TXT = "该选项只有GM可见！请选择即将开奖的结果！\r\n";
			for (var i = 0;i<NameList.length ;i++ ){
				TXT+="#L"+i+"#"+NameList[i]+"#l\r\n";
			}
			cm.sendSimple(TXT);
		}else if(selection == 3){
			var TXT = "#r说明：#k\r\n";
			TXT+="这里加说明！！！！！！！！！！！！！！！！！";
			TXT+="这里加说明！！！！！！！！！！！！！！！！！";
			TXT+="这里加说明！！！！！！！！！！！！！！！！！";
			TXT+="这里加说明！！！！！！！！！！！！！！！！！";
			TXT+="这里加说明！！！！！！！！！！！！！！！！！";
			cm.sendSimple(TXT);
			cm.dispose();
		}
	} else if (status == 2) {
		var MyName = cm.getChar().getName();
		if(Tpe == 0){
			Type = selection;
			if(getcounttype(charId,haha,Type)){
				if (XXX[1]>=List[Type][6]){
					cm.sendOk("对不起！今天已经下过"+List[Type][6]+"注#r["+List[Type][0]+"]#k了哦！");
					cm.dispose();
					return;
				}
			}else{
				cm.sendOk("未知错误！请联系作者");
				cm.dispose();
				return;
			}
			var TXT = "您当前选择 #r"+List[Type][0]+"#k 进行下注，请现在选择您的吉祥生肖吧！\r\n\r\n";
			for (var i = 0;i < NameList.length ;i++ ){
				TXT+="#L"+i+"# 我选择 #b【"+NamePH[i]+"】#k#l\r\n";
			}
			cm.sendSimple(TXT);
		}else if(Tpe == 1){
			if (XXX[2]!=XX.length){
				var A = selection;
				if (XX[A][5]!=0){
					cm.sendOk("本期开奖奖励已经领取过，请勿重复领取，你走吧！");
					cm.dispose();
					return;
				}
				if (XX[A][0]==0){
					cm.gainNX(1,XX[A][2]*(List[XX[A][0]][5]/List[XX[A][0]][4]));
				}else if(XX[A][0]==1){
					cm.gainNX(2,XX[A][2]*(List[XX[A][0]][5]/List[XX[A][0]][4]));
				}else if(XX[A][0]==2){
					cm.gainMeso(XX[A][2]*(List[XX[A][0]][5]/List[XX[A][0]][4]));
				}else if(XX[A][0]==3){
					cm.addHyPay(-XX[A][2]*(List[XX[A][0]][5]/List[XX[A][0]][4]));
				}
				if(update(charId,haha,XX[A][4])){
					cm.worldSpouseMessage(0x15, "[生肖系统]： 中奖玩家 "+MyName+" 领取了 ["+XX[A][2]*(List[XX[A][0]][5]/List[XX[A][0]][4])+"]"+List[XX[A][0]][0]);
					cm.sendOk("领奖成功！");
				}else{
					cm.sendOk("领奖失败！出现未知错误！请联系作者");
				}
				cm.dispose();
			}else{
				cm.dispose();
			}
		}else if(Tpe == 2){
			var B = selection;
			em.setProperty("change",""+B);
			cm.sendOk("设置成功！");
			cm.dispose();
		}
	} else if (status == 3) {
		if(Tpe == 0){
			Name = selection;
			cm.sendGetNumber("您当前选择的生肖为#r["+NameList[Name]+"]#k\r\n您当前选择的币种为#r["+List[Type][0]+"]#k\r\n请在下面输入你要下注的金额#r(点确定后操作不可返回！)#k：\r\n",List[Type][1],List[Type][1],List[Type][2]);
		}
	} else if (status == 4) {
		var MyName = cm.getChar().getName();
		if(Tpe == 0){
			Quantity = selection;
			if (hour==20){
				cm.sendOk("下注失败");
				cm.dispose();
				return;
			}
			if(Consume(Type,Quantity)){
				if(AddZodiac(charId,Type,Name,Quantity,haha)){
					cm.worldSpouseMessage(0x15, "[生肖系统]： 玩家 "+MyName+" 对生肖["+NameList[Name]+"] 下注了"+Quantity+" "+List[Type][0]);
					cm.sendOk("下注成功！");
				}else{
					cm.sendOk("出现未知错误！请联系作者");
				}
				cm.dispose();
			}else{
				cm.sendOk("下注失败!请检查你有没有那么多金额！");
				cm.dispose();
			}
		}
    }
}
function Consume(Type,Quantity){
	if (Type==0){
		if(cm.getNX(1)<Quantity){
			return false;
		}else{
			cm.gainNX(1,-Quantity);
			return true;
		}
	}else if(Type==1){
		if(cm.getNX(2)<Quantity){
			return false;
		}else{
			cm.gainNX(2,-Quantity);
			return true;
		}
	}else if(Type==2){
		if(cm.getMeso()<Quantity){
			return false;
		}else{
			cm.gainMeso(-Quantity);
			return true;
		}
	}else if(Type==3){
		if(cm.getHyPay(1)<Quantity){
			return false;
		}else{
			cm.addHyPay(Quantity);
			return true;
		}
	}
}
function AddZodiac(a,b,c,d,e){
	var db = cm.getConnection();
	var sql = "insert into zodiac values(0,?,?,?,?,?,0)";
	var pstmt = db.prepareStatement(sql);
	pstmt.setInt(1, a);
	pstmt.setInt(2, b);
	pstmt.setInt(3, c);
	pstmt.setInt(4, d);
	pstmt.setInt(5, e);
	var flag = pstmt.executeUpdate();
	pstmt.close();
	return flag;
}
function getdate(charId,haha){
	var i=0;
	var db = cm.getConnection();
	var pstmt = db.prepareStatement("select id,currency,type,quantity,time,get from zodiac where charid = ? AND time = ?");
	pstmt.setInt(1, charId);
	pstmt.setInt(1, haha);
	var result = pstmt.executeQuery();
	while (result.next()){
		var newList = Array();
		newList[0]=result.getInt("currency");
		newList[1]=result.getInt("type");
		newList[2]=result.getInt("quantity");
		newList[3]=result.getInt("time");
		newList[5]=result.getInt("get");
		newList[4]=result.getInt("id");
		XX.push(newList);
		i++;
	}
	if (i==0) {
		return false;
	}
	result.close();
	pstmt.close();
	return true;
}
function getcount(charId,haha){
	var db = cm.getConnection();
	var pstmt = db.prepareStatement("select count(*) from zodiac where charid = ? AND time=?");
	pstmt.setInt(1, charId);
	pstmt.setInt(2, haha);
	var result = pstmt.executeQuery();
	if (result.next()) {
		XXX[0] = result.getString("count(*)");
	} else {
		return false;
	}
	result.close();
	pstmt.close();
	return true;
}
function update(charId,haha,id){
	var db = cm.getConnection();
	var sql = "UPDATE zodiac SET get = 1 WHERE charid = ? AND time = ? AND id = ?";
	var pstmt = db.prepareStatement(sql);
	pstmt.setInt(1, charId);
	pstmt.setInt(2, haha);
	pstmt.setInt(3, id);
	var flag = pstmt.executeUpdate();
	pstmt.close();
	return flag;
}
function getcounttype(charId,haha,a){
	var db = cm.getConnection();
	var pstmt = db.prepareStatement("select count(*) from zodiac where charid = ? AND time=? AND currency = ?");
	pstmt.setInt(1, charId);
	pstmt.setInt(2, haha);
	pstmt.setInt(3, a);
	var result = pstmt.executeQuery();
	if (result.next()) {
		XXX[1] = result.getString("count(*)");
	} else {
		return false;
	}
	result.close();
	pstmt.close();
	return true;
}
function getresult(a){
	var i=0;
	var db = cm.getConnection();
	var pstmt = db.prepareStatement("select type,time from savezodiac order by time desc limit ?");
	pstmt.setInt(1, a);
	var result = pstmt.executeQuery();
	while (result.next()){
		var newList = Array();
		newList[0] = result.getString("type");
		newList[1] = result.getString("time");
		X.push(newList);
		i++;
	}
	if (i==0) {
		return false;
	}
	result.close();
	pstmt.close();
	return true;
}